import json
from v5.Page import Page
from v5.RequestItem import RequestItem


class PageJSONEncoder(json.JSONEncoder):
    def default(self, o):
        dict_ = {}
        if isinstance(o, Page):
            dict_['page'] = self.default(o.get_request())
            dict_['child_pages'] = [self.default(page) for page in o.get_child_pages()]
            return dict_
        elif isinstance(o, RequestItem):
            return o.get()['url']
        else:
            return json.JSONEncoder.default(self, o)

